﻿using System;
using System.Collections.Generic;
using System.Text;
using SdmxMl.Common;
using SdmxMl.Structure;

namespace SdmxMl.Helper
{
    /// <summary>
    /// Helper to compare two flat codelists
    /// </summary>
    public class FlatItemListCompare
    {
        private FlatHierarchicalList lhList;
        private FlatHierarchicalList rhList;

        public FlatItemListCompare(FlatHierarchicalList left, FlatHierarchicalList right)
        {
            lhList = left;
            rhList = right;

            // Clear any previous marker
            foreach (ISdmxHierarchyListItem item in lhList)
            {
                AnnotationList anl = item.AnnotationList.GetAllAnnotationOfType("CP_MISS", null);
                foreach (Annotation a in anl)
                    item.AnnotationList.Remove(a);
            }
        }


        public bool Compare(string[] arrLang)
        {
            bool sameCodes = false;
            if (lhList.Count == rhList.Count)
            {
                sameCodes = true;
                for (int c = 0; c < lhList.Count; c++)
                {
                    if (lhList[c].Id != rhList[c].Id)
                    {
                        sameCodes = false;
                        lhList[c].AnnotationList.Add(new Annotation("CP_MISS"));
                    }
                    else
                    {
                        foreach (string lg in arrLang)
                        {
                            if (lhList[c].Name.GetForLanguage(lg) != rhList[c].Name.GetForLanguage(lg))
                            {
                                sameCodes = false;
                                Annotation a = new Annotation("CP_MISS");
                                a.Title = lg;
                                lhList[c].AnnotationList.Add(a);
                            }
                        }
                    }
                }
            }

            return sameCodes;
        }

    }
}
